<?php
/**
 * 购物车操作Action
 * @author tangbl
 *
 */
class CartAction extends CommonAction {
	
	/**
	 * 用户购物车
	 * 异步保存选中的商品
	 */
	public function mycart(){
		
		if(IS_AJAX){
			
			$values = $this->_param("values");
			session(C('CART_SELECTED'),$values);
			session(C('CART_TABLE'),"Product");
			$this->success("add");
			
		}else{
			
			$model  = D("MemberCart");
			session(C("CART_SELECTED"),NULL);
			
			$list   = $model->getUserCart();
			$this->assign('list',$list);
			
			$this->assign("empty",$this->fetch("Empty:cart"));            
			
			$this->display();	
		}
	}
	
	/**
	 * 添加购物车
	 * @param $id  int 商品编号
	 * @param $num int 商品数量
	 */
	public function addcart(){
		
		$model= D("MemberCart");
		
		$id  = $this->_param("id","intval",0);
		$num = $this->_param("num","intval",1);
		
		if($id){
			$res = $model->addcart($id,$num);
			
			$this->assign("jumpUrl",U('Cart/mycart'));
			$this->success($res['info']);
		}else{
			$this->error(L('cat_ele_addfail'));
		}
		
	}
	
	/**
	 * 删除购物车
	 * @param $id  int 购物车主键编号
	 */
	public function delcart(){
		
		$id    = $this->_param("id","intval",0);
		
		if($id){
			$model = D("MemberCart");
			$res   = $model->delcart($id);
			$array = $model->getUserCart(false,$id);
			
			$this->ajaxReturn($array,"",1);
		}else{
			
			$this->ajaxReturn(1,"",-1);
		}
		
	}
	
	/**
	 * 改变购物车数量
	 * @param $id  int 购物车主键编号
	 */
	public function change(){
		
		$model = D("MemberCart");
		$id    = $this->_param("id");
		$num   = $this->_param("num","intval",1);
		
		if($id){
			$res   = $model->change($id,$num);
			$array = $model->getUserCart(false,$id);
			$this->ajaxReturn($array,"",1);
			
		}else{
			$this->ajaxReturn(1,"",-1);
		}
		
	}
	
	/**
	 * 清空购物车
	 */
	public function remove(){
		
		$model = D("Cart");
		
		$count = $model->remove();
		
		$this->assign("jumpUrl",U('Cart/shopcart'));
		$this->success(L('cat_ele_clearcart'));
	}
	
	
	/**
	 * 添加到收藏
	 * @param $id  int 商品编号
	 */
	public function addcollect(){
		
		$pid   = $this->_get('id',"intval","0");

		if($pid){
			$model = D("MemberCollect");
			$flag  = $model->addCollect($pid);
			if($flag){
				$this->success(L('cat_ele_addcollect'));
			}else{
				$this->error(L('cat_ele_addcollect1'));
			}
		}else{
			$this->error(L('cat_ele_addcollect1'));
		}
	} 
	

	/**
	 * 商城顶部购物车(ajax)
	 */
	public function cart_header(){
		
		$model = D("MemberCart");
		$list  = $model->getUserCartList();
		
		$this->assign("list",$list);
		$this->assign("count",count($list));
		
		$this->assign("empty","您的购物车是空的");
		
		$this->display("cart_header");
	}
	
	/**
	 * 购物车为空
	 */
	public function cart_empty(){
		$this->display("Empty:cart");
	}
	
	/**
	 * 团购商品列表
	 * 异步团购处理
	 */
	public function tuan(){
		
		if(IS_AJAX){
			
			$id      = $this->_get("id","intval");
			$num     = $this->_get("num","intval",1);
			$user    = getMember();
			
			//检查团购条件
			$user['pid'] = $id;
			$user['num'] = $num;
			$json = sendRest("Activity/checkGroupou",$user);
			
			if($json['status'] == 1){
				session(C('CART_SELECTED'),$id.",".$num);
				session(C('CART_TABLE'),   "Groupou");
			}
			$this->ajaxReturn($json);
			
		}else{
			
			$order = "id desc";
			$model = D("GroupouView");
			
			$map['type'] = 2;
			$map['lang'] = getLang();
			$map['begintime'] = array('LT',time());
			
			$rows  = $model->where($map)->order($order)->select();
			
			$this->assign("time",time());
			$this->assign("list",$rows);
			$this->display();
		}
	}
	
	/**
	 * 团购详情
	 */
	public function tdetail(){
	
		$model = D("GroupouView");
		$map['lang'] = getLang() ;
		$map['type'] = 2;
		$map['pid']  = $this->_param("id",intval,0);
	
		$rows  = $model->where($map)->find();
		$this->assign("view",$rows);
	
		$product = M("Product")->find($rows['pid']);
		$this->assign("detail",$product);
	
		$this->assign("time",time());
		$this->display();
	}
	
	/**
	 * 秒杀商品列表
	 * 异步秒杀处理
	 */
	public function miao(){
		
		if(IS_AJAX){
				
			$id      = $this->_get("id","intval");
			$num     = $this->_get("num","intval",1);
			$user    = getMember();
				
			//检查秒杀条件
			$user['pid'] = $id;
			$user['num'] = $num;
			$json = sendRest("Activity/checkSeckill",$user);
				
			if($json['status'] == 1){
				session(C('CART_SELECTED'),$id.",".$num);
				session(C('CART_TABLE'),   "Seckill");
			}
			$this->ajaxReturn($json);
				
		}else{
			
			$model = D("SeckillView");
			$map['lang'] = getLang();
			$map['type'] = 3;
			$rows  = $model->where($map)->order("sort desc")->select();
			
			$this->assign("list",$rows);
			$this->assign("time",time());
			
			$this->display();
		}
	}
	
	/**
	 * 秒杀详情
	 */
	public function mdetail(){
		
		$model  = D("SeckillView");
		$map['lang'] = getLang() ;
		$map['type'] = 3;
		$map['pid']  = $this->_param("id",intval,0);
		
		$rows  = $model->where($map)->find();
		$this->assign("view",$rows);
	
		$product = M("Product")->find($rows['pid']);
		$this->assign("detail",$product);
	
		$this->assign("time",time());
		$this->display();
	}
}
?>